package com.goldcontrol.cloudweb.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.goldcontrol.cloudweb.service.ApiService;
import com.goldcontrol.cloudweb.util.HttpUtil;
import com.goldcontrol.cloudweb.weixin.service.WeixinServiceApi;
import org.apache.log4j.Logger;
import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Comparator;

@Controller
@RequestMapping("api")
public class ApiController {

    private static Logger logger = Logger.getLogger(WechatQuartzController.class);

    @Autowired
    ApiService apiService;

    @Autowired
    WeixinServiceApi weixinServiceApi;

    @RequestMapping(value = "/getProjectsList",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getProjectsList(@RequestParam String token) throws ParseException {
        String result = apiService.getProjectsList(token);
        return result;
    }

    @RequestMapping(value = "/getCityWeather",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getCityWeather(@RequestParam String longitude,@RequestParam String latitude){
        JSONObject weaInfo = apiService.getCityTemNew(longitude,latitude).getJSONObject("weaInfo");
        return weaInfo.getJSONArray("HeWeather6").getJSONObject(0).toJSONString();
    }

    /**
     * 数据概览接口
     * @param token
     * @param projectId
     * @return
     */
    @RequestMapping(value = "/getProjectInfo",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getProjectInfo(@RequestParam String token,@RequestParam String projectId){
        String result = apiService.getProjectInfo(token,projectId);
        return result;
    }

    @RequestMapping(value = "/getProjectCurrentItemData",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getProjectCurrentItemData(@RequestParam String token,@RequestParam String projectId){
        String result = apiService.getProjectCurrentItemData(token,projectId);
        return result;
    }

    @RequestMapping(value = "/getProjectCurrentItemDataNew",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getProjectCurrentItemDataNew(@RequestParam String token,@RequestParam String projectId){
        JSONObject resultData = new JSONObject();
        try{
            String result = apiService.getProjectCurrentItemData(token,projectId);
            Integer control = weixinServiceApi.getProjectControl(Integer.parseInt(projectId));
            Integer onff = weixinServiceApi.getProjectOnOff(Integer.parseInt(projectId));
            resultData.put("control",control);
            resultData.put("onff",onff);
            if(StringUtils.isNotBlank(result)
                    &&  JSONObject.parseObject(result) != null
                    &&  JSONObject.parseObject(result).getJSONArray("data") != null){
                JSONArray jsonArray = JSONObject.parseObject(result).getJSONArray("data");
                jsonArray.sort(Comparator.comparingInt(o -> ((JSONObject) o).getIntValue("itemid")));
                for(int i=0;i<jsonArray.size();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    resultData.put(jsonObject.getString("alias"),jsonObject);
                }

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("data",resultData);
                jsonObject.put("items",jsonArray);
                return JSON.toJSONString(jsonObject, SerializerFeature.DisableCircularReferenceDetect);
            }else{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("status",200);
                return jsonObject.toJSONString();
            }
        }catch (Exception e){
            logger.error("getProjectCurrentItemDataNew error {}",e);
            resultData.put("status",200);
        }
        return resultData.toString();
    }

    @RequestMapping(value = "/getVdevicesItems",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getVdevicesItems(@RequestParam String token,@RequestParam String projectId){
        String result = apiService.getVdevicesItems(token,projectId);
        return result;
    }

    @RequestMapping(value = "/initData",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initData(@RequestParam String token,Integer projectId) throws ParseException {
        return apiService.initDataOne(token,projectId);
    }

}
